package com.mspbots.sync.wise.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.mspbots.sync.wise.service.TimeEntriesService;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.Map;

/**
 * com.mspbots.sync.wise.controller.TimeController
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/10/18
 */
@Log4j2
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "times")
public class TimeEntryController {

    private final TimeEntriesService entryService;

    @GetMapping("page")
    public Mono<JsonNode> getPage(@RequestParam Map<String, String> params) {
        log.info("times/page:params:{}", params);
        Assert.notNull(params.get("tenantId"), () -> "Search param tenant id not null.");
        return this.entryService.getPage(params);
    }

    @GetMapping("/ticket/entries")
    public Mono<JsonNode> getList(@RequestParam Map<String, String> params) {
        Assert.notNull(params.get("tenantId"), () -> "Search param tenant id not null.");
        Assert.notNull(params.get("ticketId"), () -> "Search param ticket id not null.");
        params.put("conditions", " (chargeToType='ServiceTicket' OR chargeToType='ProjectTicket') " +
                "AND chargeToId=" + params.get("ticketId"));
        params.put("pageSize", "1000");
        params.put("orderBy", "id desc");
        return this.entryService.getPage(params);
    }
}
